package cn.hchaojie.camp.day02.exercise;

/**
 * 写一个方法，传入一个整型数组，判断数组里面是否有4个连续相等的整数。
 * 
 * @author hchaojie
 *
 */
public class ArrayHasContinuousFour {
	public static void main(String[] args) {
		int[] numbers1 = {1, 2, 2, 2, 1, 3, 3, 4};
		int[] numbers2 = {1, 2, 2, 2};
		int[] numbers3 = {1, 2, 2, 2, 1, 3, 3, 3, 3};
		int[] numbers4 = {1, 2, 2, 2, 1, 3, 3, 3, 3, 5};
		
		System.out.println(hasContinuousFour(numbers1));
		System.out.println(hasContinuousFour(numbers2));
		System.out.println(hasContinuousFour(numbers3));
		System.out.println(hasContinuousFour(numbers4));
	}

	public static boolean hasContinuousFour(int[] numbers) {
		int count = 1;	// 计算当前有几个连续相等的了
		for (int i = 1; i < numbers.length - 1; i++) {
			if (numbers[i] != numbers[i + 1]) {
				count = 1;	// 下一个位置不想当，重置计数
			} else {
				count++;
				
				if (count == 4) {
					return true;
				}
			}
		}

		return false;
	}
}
